<template>
    <div>
<!--        <el-form-item label="用户ID">-->
<!--            <el-input v-model="formFilter.userId" placeholder="用户ID"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="手机号">-->
<!--            <el-input v-model="formFilter.phone" placeholder="手机号"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="用户名">-->
<!--            <el-input v-model="formFilter.userName" placeholder="用户名"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="用户等级">-->
<!--            <el-select v-model="formFilter.level" placeholder="用户等级">-->
<!--                <el-option-->
<!--                        v-for="item in options"-->
<!--                        :key="item"-->
<!--                        :label="item"-->
<!--                        :value="item">-->
<!--                </el-option>-->
<!--            </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="注册时间">-->
<!--            <datePicker v-model="formFilter.date"></datePicker>-->
<!--        </el-form-item>-->
<!--        <el-form-item>-->
<!--            <el-button type="primary" @click="queryFn" icon='el-icon-search'>查 询</el-button>-->
<!--            <el-button @click="$emit('onRest')" icon='el-icon-refresh'>重 置</el-button>-->
<!--        </el-form-item>-->
        <el-upload
                style="display: inline-block; margin-right: 10px;"
                action="/upload/file"
                :http-request="upload"
                :show-file-list='false'
                multiple>
            <el-button type="success" plain size="mini">导入账号</el-button>
        </el-upload>
<!--        <el-button type="success" plain size="mini" @click="$emit('export')">导出账号</el-button>-->
<!--        <el-button type="success" plain size="mini" @click="$emit('del')">删除账号</el-button>-->
        <el-input v-model="num" placeholder="注册个数" size="mini" style="width: 200px;"></el-input>
        <el-button type="success" plain size="mini" @click="batchRegister">批量注册</el-button>
        <el-button type="success" plain size="mini" @click="batchLogin">批量登录</el-button>
    </div>
</template>

<script>
  import datePicker from '@/components/datePicker'
  import {upload, batchRegister, batchLogin} from '@/api/users'

  export default {
    name: "tool",
    components: {
      datePicker
    },
    data () {
      return {
        options: [1, 2, 3, 4, 5],
        num: ''
      }
    },
    props: {
      formFilter: Object,
      get: Function
    },
    methods: {
      queryFn () {
        this.get()
      },
      upload(config) {
        const formData = new FormData()
        formData.append('file', config.file)
        formData.append('type', '10')
        return upload(formData).then(() => {
          this.$message.success('导入文件成功')
          this.get()
        })
      },
      /**
       * 批量注册
       * @returns {Promise<void>}
       */
      async batchRegister () {
        try {
          const params = {
            num: this.num
          }
          const {data} = await batchRegister(params)
          this.$message.success('批量注册成功')
          this.get()
        }catch (e) {
          console.log(e)
        }
      },
      /**
       * 批量登录
       * @returns {Promise<void>}
       */
      async batchLogin () {
        try {
          let arr = []
          const allSelection = this.$store.state.user.allSelection
          const currentSelection = this.$store.state.user.currentSelection
          if(allSelection.length === currentSelection.length) {
            arr = ['-1']
          } else {
            if(!currentSelection.length) {
              this.$message.warning('没有选择用户')
              return
            }
            arr  = currentSelection.map(i => i.id)
          }
          const params = {
            userIds: arr
          }
          const {data} = await batchLogin(params)
          this.$message.success('批量登录成功')
          this.get()
        }catch (e) {
          console.log(e)
        }
      }
    }
  }
</script>

<style scoped>

</style>
